package cn.mdmbct.douyin.spring.example.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -2312565066153168006L;

    /**
     * 总条数
     */
    private Long total;

    /**
     * 总页数
     */
    private int  totalPage;

    /**
     * 当前页
     */
    private int currentPage;

    /**
     * 每页显示数
     */
    private int pageSize;
    private int start;
    private int end;

    /**
     * 当前页数据
     */
    private List<T> items;

    public PageResult(Long total, int totalPage, int currentPage, List<T> items){
        this.total=total;
        this.totalPage=totalPage;
        this.currentPage=currentPage;
        this.items=items;
    }
    public PageResult(Long total, List<T> items){
        this.total=total;
        this.items=items;
    }

    public PageResult(Long total, int totalPage, List<T> items){
        this.total=total;
        this.items=items;
       this.totalPage = totalPage;
    }
    public List<T> getCurrPageList(List<T> list, Integer currentPage, Integer pageSize) {
        int begin = (currentPage - 1) * pageSize;
        int size = list.size();
        int end = begin + pageSize;
        if (size <= end) {
            end = size;
        }
        return list.subList(begin, end);
    }

    /**
     * 返回一个空list的PageResult实例
     */
    public static <T> PageResult<T> pageResultWithNullList(int pageSize) {
        return PageResult.<T>builder().start(0).end(0).total(0L).totalPage(0)
                .pageSize(pageSize)
                .currentPage(1).items(new ArrayList<>()).build();
    }
}
